package cn.cqautotest.cqviehelper.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import cn.android52.filepicker.ui.activity.FilePickerActivity;
import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.base.BaseActivity;
import cn.cqautotest.cqviehelper.config.SkinConstants;
import cn.cqautotest.cqviehelper.skin.SkinManager;
import cn.cqautotest.cqviehelper.skin.callback.ISkinChangingCallback;
import cn.cqautotest.cqviehelper.ui.dialog.ToastDialog;
import cn.cqautotest.cqviehelper.utils.CacheDataManager;
import cn.cqautotest.cqviehelper.ui.dialog.MenuDialog;

import com.hjq.base.action.AnimAction;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.widget.layout.SettingBar;
import com.hjq.widget.view.SwitchButton;
import com.thl.filechooser.FileChooser;
import com.thl.filechooser.FileInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.cqautotest.cqviehelper.utils.UpdateAppDataUtils;

/**
 * author : Android 轮子哥
 * github : https://gitee.com/anjiemo/CqvieHelper
 * time   : 2019/03/01
 * desc   : 设置界面
 */
@SuppressLint("NonConstantResourceId")
public final class SettingActivity extends BaseActivity
        implements SwitchButton.OnCheckedChangeListener {

    @BindView(R.id.container)
    View container;
    @BindView(R.id.sb_setting_cache)
    SettingBar mCleanCacheView;
    @BindView(R.id.sb_setting_switch)
    SwitchButton mAutoSwitchView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected View getLayoutContainer() {
        return container;
    }

    @Override
    protected void initView() {
        // 设置切换按钮的监听
        mAutoSwitchView.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        // 获取应用缓存大小
        mCleanCacheView.setRightText(CacheDataManager.getTotalCacheSize(this));
    }

    @OnClick({R.id.sb_setting_language, R.id.sb_setting_change_skin, R.id.sb_setting_update, R.id.sb_setting_agreement,
            R.id.sb_setting_about, R.id.sb_setting_cache, R.id.sb_setting_auto, R.id.sb_setting_exit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sb_setting_language:
                // 底部选择框
                new MenuDialog.Builder(this)
                        // 设置点击按钮后不关闭对话框
                        //.setAutoDismiss(false)
                        .setList(R.string.setting_language_simple, R.string.setting_language_complex)
                        .setListener((MenuDialog.OnListener<String>) (dialog, position, string) ->
                                BrowserActivity.start(getActivity(), "https://github.com/getActivity/MultiLanguages"))
                        .setGravity(Gravity.BOTTOM)
                        .setAnimStyle(AnimAction.BOTTOM)
                        .show();
                break;
            case R.id.sb_setting_change_skin:
                XXPermissions.with(this).permission(Permission.Group.STORAGE).request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        if (all) {
                            startActivity(new Intent(getActivity(), FilePickerActivity.class));
                            // FileChooser fileChooser = new FileChooser(SettingActivity.this, filePath -> {
                            //     SkinManager.getInstance().changeSkin(filePath, SkinConstants.SKIN_PLUGIN_PKG,
                            //             new ISkinChangingCallback() {
                            //                 @Override
                            //                 public void onStartChangeSkin() {
                            //
                            //                 }
                            //
                            //                 @Override
                            //                 public void onChangeSkinError(Exception e) {
                            //                     ToastUtils.showShort("换肤失败，请重新选择皮肤包文件！");
                            //                 }
                            //
                            //                 @Override
                            //                 public void onChangeSkinComplete() {
                            //                     ToastUtils.showShort("换肤成功！");
                            //                 }
                            //             });
                            // });
                            // fileChooser.setChooseType(FileInfo.FILE_TYPE_FILE);
                            // fileChooser.open();
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        ToastUtils.showShort("请授予读写手机存储权限！");
                    }
                });
                break;
            case R.id.sb_setting_update:
                //检测APP更新
                UpdateAppDataUtils.updateApp(this);
                break;
            case R.id.sb_setting_agreement:
                BrowserActivity.start(this, "https://github.com/getActivity/Donate");
                break;
            case R.id.sb_setting_about:
                startActivity(AboutActivity.class);
                break;
            case R.id.sb_setting_auto:
                // 自动登录
                mAutoSwitchView.setChecked(!mAutoSwitchView.isChecked());
                break;
            case R.id.sb_setting_cache:
                // 清除内存缓存（必须在主线程）
                Glide.get(getActivity()).clearMemory();
                new Thread(() -> {
                    // 清除本地缓存（必须在子线程）
                    Glide.get(getActivity()).clearDiskCache();
                }).start();
                CacheDataManager.clearAllCache(this);
                mHandler.postDelayed(() -> {
                    // 重新获取应用缓存大小
                    mCleanCacheView.setRightText(CacheDataManager.getTotalCacheSize(getActivity()));
                }, 500);
                break;
            case R.id.sb_setting_exit:
//                if (true) {
//                    startActivity(LoginActivity.class);
//                    // 进行内存优化，销毁除登录页之外的所有界面
//                    ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
//                    return;
//                }
//                // 退出登录
//                EasyHttp.post(this)
//                        .api(new LogoutApi())
//                        .request(new HttpCallback<HttpData<Void>>(this) {
//
//                            @Override
//                            public void onSucceed(HttpData<Void> data) {
//                                startActivity(LoginActivity.class);
//                                // 进行内存优化，销毁除登录页之外的所有界面
//                                ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
//                            }
//                        });
                break;
            default:
                break;
        }
    }

    /**
     * {@link SwitchButton.OnCheckedChangeListener}
     */
    @Override
    public void onCheckedChanged(SwitchButton button, boolean isChecked) {
        //ToastUtils.showShort(isChecked ? "已开启" : "已关闭");
    }
}